﻿<%@ include file="/WEB-INF/mmsh/start.jspf"%>
<html>
<head>
<%@ include file="/WEB-INF/jspf/meta.jspf"%>
<title>物资项目</title>
<%@ include file="/WEB-INF/mmsh/assets.jspf"%>
</head>
<body>
	<div class="placebox">
		<strong>您的位置：</strong>检修改造项目计划<span>&raquo;</span>数据字典
	</div>
	<div class="datablebox clearfix mb10">
		<div class="mod_blue">
			<h1>
				<strong>数据字典</strong> <span class="btnbox r"> <input
					type="button" class="btnstyle btn_50" runat="server" id="BtAdd"
					value="新增" />&nbsp;&nbsp;<input type="button"
					class="btnstyle btn_50" id="BtDel" value='禁用' />
				</span><a href="#refresh" class="none">Refresh</a> <a href="#current-link"
					style="display: none" id="self-link"></a>
			</h1>
			<div class="conn datablebox">
				<div class="data_parambox mb3" style="height: 35px;">
					<sql:query var="categories" dataSource="${mmsh}">
						select distinct category from mmsh_enumitem order by category asc
					</sql:query>
					<span>类别: <select id="ddlCategories">
						<c:forEach var="category" items="${categories.rows}">
							<option value="<c:out value="${category.category}"></c:out>" ><c:out value="${category.category}"></c:out></option>
						</c:forEach>
					</select>
					</span>
				</div>
				<div style="overflow-x: auto; overflow-y: hidden; width: 970px;">
					<table cellspacing="0" cellpadding="0" class="datable01">
						<thead>
							<tr>
								<th scope="col"><input name="chkAll" id="chkAll"
									type="checkbox" /></th>
								<th scope="col">序号</th>
								<th scope="col">类别</th>
								<th scope="col">文本</th>
								<th scope="col">排序</th>
								<th scope="col">默认值</th>
								<th scope="col">启用</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<c:set var="cat" value="${param.cat}"></c:set>
							<c:if test="${empty cat }">
								<sql:query var="firstCategory" dataSource="${ mmsh }">
									select category from mmsh_enumitem order by category asc limit 1
								</sql:query>
								<c:choose>
									<c:when test="${firstCategory.rowCount==0 }">
										<c:set var="cat" value="" />
									</c:when>
									<c:otherwise>
										<c:set var="cat" value="${firstCategory.rows[0].category }" />
									</c:otherwise>
								</c:choose>
							</c:if>
							<sql:query var="items" dataSource="${mmsh }">
								select row_number() over(order by a.name asc) as seq,a.* from mmsh_enumitem a where category=?
								<sql:param>${ cat }</sql:param>
							</sql:query>
							<c:forEach var="row" items="${items.rows }">
								<tr>
								<td><input type="checkbox" value="<c:out value="${row.id }" />"/></td>
								<td><c:out value="${row.seq }" /></td>
								<td><c:out value="${row.category }"></c:out></td>
								<td><c:out value="${row.name }"></c:out></td>
								<td><c:out value="${row.sort_order }"></c:out></td>
								<td><c:out value="${row.is_default }"></c:out></td>
								<td><c:out value="${row.enabled }"></c:out></td>
								<td><a href="#<c:out value="${row.id}" />">编辑</a></td>
								</tr>
							</c:forEach>
							<tr style='background-color: #F2F2F2'>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
	<div class="popwin_box" id="divProjectAdd">
		<div class="popwin_close">
			<a href="javascript:void(0);" class="close-div" title="关闭窗口"></a>
		</div>
		<h1 class="title">新增/修改数据字典</h1>
		<form id="mp-form" action="#" method="post">
			<div>
				<table border="0" cellspacing="0" cellpadding="0">
					<tr>
						<th align="right"><label> 类别：</label></th>
						<td align="left"><input type="hidden" id="id" name="ObjectID" />
							<input type="text" class="required" style="width: 80%;"
							maxlength="100" id="Category" name="Category" /><span
							style="color: Red; font-weight: bold"> * </span></td>
					</tr>
					<tr>
						<th align="right"><label> 编码：</label></th>
						<td align="left"><input id="Code" name="Code"
							style="width: 250px;"><span
							style="color: Red; font-weight: bold"> * </span></td>
					</tr>
					<tr>
						<th align="right"><label> 显示文本：</label></th>
						<td align="left"><input type="text" style="width: 250px;"
							id="EnumText" name="EnumText" /><span
							style="color: Red; font-weight: bold"> * </span></td>
					</tr>
					<tr>
						<th align="right"><label> 排序：</label></th>
						<td align="left"><input type="text" name="SortOrder"
							style="width: 250px;" id="SortOrder" value="1" /><span
							style="color: Red; font-weight: bold"> * </span></td>
					</tr>
					<tr>
						<th align="right"><label> 默认值：</label></th>
						<td align="left"><input type="checkbox" name="IsDefault"
							id="IsDefault" value="1" /></td>
					</tr>
					<tr>
						<th align="right"><label> 启用：</label></th>
						<td align="left"><input type="checkbox" name="Status"
							id="Status" value="1" checked="checked" /></td>
					</tr>
					<tr>
						<th></th>
						<td>
							<button type="reset" id="mp-form-reset" class="none">
								Reset</button> <input type="button" value="提交" id="tdSumit" /><label>&nbsp;&nbsp;&nbsp;</label><label>&nbsp;&nbsp;&nbsp;</label><input
							type="button" class="close-div" value="关闭窗口" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<script type="text/javascript">
    $(document).ready(function () {
        var url = 'current-url';
        $(".edit-item").click(function (e) {
            e.preventDefault();
            $.getJSON(url, { action: 'get', id: $(this).attr('href') }, function (data) {
                for (var key in data) {
                    if (data.hasOwnProperty(key)) {
                        if (key === 'IsDefault') {
                            if (data[key]) {
                                $("#IsDefault").attr('checked', 'checked');
                            } else {
                                $("#IsDefault").removeAttr('checked');
                            }
                            continue;
                        }
                        if (key === 'Status') {
                            if (data[key]) {
                                $("#Status").attr('checked', 'checked');
                            }
                            else {
                                $("#Status").removeAttr('checked');
                            }
                            continue;
                        }
                        $('[name="' + key + '"]').val(data[key]);
                    }
                }
            });
            $("#Code").attr('readonly', 'readonly');
            var p = $("#divProjectAdd");
            if (!$("#mp-form").length) {
                $("table", p).wrap('<form id="mp-form"></form>');
            }
            $("#mp-form-reset").click();
            $("#divProjectAdd").jqm({ overlay: 40, modal: true, toTop: true });
            $("#divProjectAdd").jqmShow();
        });
        $("#btn-add").click(function (e) {
            e.preventDefault();
            var p = $("#divProjectAdd");
            if (!$("#mp-form").length) {
                $("table", p).wrap('<form id="mp-form"></form>');
            }
            $("#Code").removeAttr('readonly');
            $("#mp-form-reset").click();
            $("#divProjectAdd").jqm({ overlay: 40, modal: true, toTop: true });
            $("#divProjectAdd").jqmShow();
        });
        $("#BtDel").click(function () {
            if ($(".checkbox:checked").length) {
                var ids = $.map($(".checkbox:checked"), function (item) {
                    return $(item).val();
                });
                $.post(url + "?action=disable", { ids: ids.join(',') }, function (data) {
                    if (data && data.isSuccess) {
                        $("#btn-refresh").click();
                    }
                });

            } else {
                alert('请选择');
            }
        });
        $("#chkAll").click(function () {
            $("input[name='chkList']").attr("checked", $("#chkAll").attr("checked")); ;
        });
        $("tbody .checkbox").click(function () {
            var flag = true;
            $("input[name='chkList']").each(function () {
                if (!this.checked)
                    flag = false;
            });
            $("#chkAll").attr("checked", flag);
        });
        function validate() {
            var category = $.trim($("#Category").val());
            if (!category || !category.length) {
                alert('类别不能为空');
                return false;
            }
            var code = $.trim($("#Code").val());
            if (!code || !code.length) {
                alert('编码不能为空');
                return false;
            }
            var enumText = $.trim($("#EnumText").val());
            if (!enumText || !enumText.length) {
                alert('显示文本不能为空');
                return false;
            }
            var sortOrder = $.trim($("#SortOrder").val());
            if (!sortOrder || !sortOrder.length) {
                alert('排序不能为空');
                return false;
            }
            var b = /^-?\d*$/;
            if (!b.test(sortOrder)) {
                alert('排序必须为整数');
                return false;
            }
            var m = parseFloat(sortOrder);
            if (isNaN(m)) {
                alert('排序必须为整数');
                return false;
            };
            return true;
        }

        $(".is-default").click(function (e) {
            var self = $(this),
                id = self.val(),
                isDefault = self.is(":checked") ? 1 : 0;
            $.post(url + '?action=default', { id: id, isDefault: isDefault }, function (data) {
                if (data && data.isSuccess) {
                    $("#btnfresh").click();
                } else {
                    alert('设置/取消默认出错');
                }
            });
        });
        $(".enabled").click(function (e) {
            var self = $(this),
                id = self.val(),
                enabled = self.is(":checked") ? 1 : 0;
            $.post(url + '?action=enable', { id: id, enabled: enabled }, function (data) {
                if (data && data.isSuccess) {
                    $("#btnfresh").click();
                } else {
                    alert('启用/禁用出错');
                }
            });
        });
        $("#tdSumit").click(function () {
            if (!validate()) {
                return false;
            }
            var a = $("#mp-form").serializeArray();
            var e = {};
            $.each(a, function (i, item) {
                e[item.name] = item.value;
            });
            $.post(url + '?action=post', e, function (data) {
                if (data && data.isSuccess) {
                    $("#self-link").attr('href', url + '?cat=' + e.Category)[0].click();
                } else {
                    alert(data.message || '错误,请重试');
                }
            });
            return false;
        });
    });
    $(".close-div").click(function () {
        $("#divProjectAdd").jqmHide();
    });
	</script>
</body>
</html>